﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ExpertMIS.Brokers;
using SYS.Basic;
using System.Collections;
using ExpertMIS.Entities;

namespace ExpertMIS
{
    public partial class QuestionsAuditList : ExpertMIS.Codes.AdminAuthenticationPage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            pgvQuestions.DeleteClientScript = "if(!confirm('您确定要删除该问题吗？')) return false; ";
            pgvQuestions.EditClientURL = "~/Admin/QuestionsForm.aspx?oid={0}&search=F_QuestionOID={0}";
            pgvQuestions.AuditClientURL = "~/Admin/QuestionsAuditList.aspx?oid={0}&action=audit&passed={1}";
            pgvQuestions.DeleteClientURL = "~/Admin/QuestionsAuditList.aspx?oid={0}&action=delete";
            pgvQuestions.ObjectName = "ExpertMIS.QuestionListView";
            pgvQuestions.IsLoadData = true;
            pgvQuestions.CheckFieldName = "F_Status";

            if (!IsPostBack)
            {
                RefreshDomainn();
                RefreshExpert();
                // 这里需要再次判断，如果为客户端脚本制定window.location.href,则IsPostBack也为false
                if (string.IsNullOrEmpty(Request["search"]))
                {
                    // 页面初次加载，则不需要load数据
                    pgvQuestions.IsLoadData = false;
                }
            }

            string action = Request["action"];
            if (!string.IsNullOrEmpty(action))
            {
                string oid = Request["oid"];
                string passed = Request["passed"];
                switch (action)
                {
                    case "audit":
                        bool bCheck = false;
                        if (passed.ToString() == "0")
                        {
                            bCheck = true;
                        }
                        AuditQuestion(oid as string, bCheck);
                        break;
                    case "delete":
                        DeleteQuestion(oid);
                        break;
                    case "query":
                        //pgvQuestions.QueryResult();
                        pgvQuestions.IsLoadData = true;
                        break;
                    case "batchAudit":
                        string quesIDs = null;
                        if (Request["quesIDList"] != null)
                        {
                            quesIDs = Request["quesIDList"].Replace("{", "").Replace("}", "");
                        }

                        if (quesIDs != null)
                        {
                            QuestAuditBatch(quesIDs.Split(','));
                        }
                        break;
                    case "batchDelete":
                        string delIDs = null;
                        if (Request["quesIDList"] != null)
                        {
                            delIDs = Request["quesIDList"].Replace("{", "").Replace("}", "");
                        }

                        if (delIDs != null)
                        {
                            QuestDeleteBatch(delIDs.Split(','));
                        }
                        break;
                    default:
                        break;
                }
            }
        }

        /// <summary>
        /// 刷新用户列表
        /// </summary>
        public void RefreshExpert()
        {
            ADMUserBroker broker = new ADMUserBroker();
            // wdong@2009-09-12, 修正获取所有专家用户，原先为获取所有用户
            IList<ADMUserEntity> userList = broker.GetAllExpertUsers();

            if (userList != null && userList.Count > 0)
            {
                ADMUserEntity entity = new ADMUserEntity();
                entity.OID = "-1";
                entity.Name = "全部";
                userList.Insert(0, entity);

                ddlExpert.DataSource = userList;
                ddlExpert.DataTextField = "Name";
                ddlExpert.DataValueField = "OID";
                ddlExpert.DataBind();

            }
        }

        /// <summary>
        /// 刷新行业领域
        /// </summary>
        public void RefreshDomainn()
        {
            DomainBroker broker = new DomainBroker();
            IList<DomainEntity> domainList = broker.GetAllDomains();
            if (domainList != null && domainList.Count > 0)
            {
                DomainEntity domain = new DomainEntity();
                domain.OID = "-1";
                domain.DomainName = "全部";
                domainList.Insert(0, domain);

                ddlDomain.DataSource = domainList;
                ddlDomain.DataTextField = "DomainName";
                ddlDomain.DataValueField = "OID";
                ddlDomain.DataBind();
            }
        }

        protected void AuditQuestion(string OID, bool checkedValue)
        {
            try
            {
                QuestionBroker broker = new QuestionBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                QuestionEntity entity = broker.GetEntity(hsQuery) as QuestionEntity;

                if (entity == null || entity.Status != QuestionStatus.UnPassed)
                {
                    return;
                }

                if (!checkedValue)
                {
                    return;
                }

                // TODO，wdong，对于提交问题来说，只要审核通过就不允许再次更改其状态
                entity.Status = QuestionStatus.Unsolve;

                //if (checkedValue)
                //{
                //    entity.Status = QuestionStatus.UnPassed;
                //}
                //else
                //{
                //    entity.Status = QuestionStatus.Unsolve;
                //}

                broker.SaveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }

        protected void DeleteQuestion(string OID)
        {
            try
            {
                QuestionBroker broker = new QuestionBroker();
                Hashtable hsQuery = new Hashtable();
                hsQuery.Add("OID", OID);
                QuestionEntity entity = broker.GetEntity(hsQuery) as QuestionEntity;

                if (entity == null)
                {
                    return;
                }
                broker.RemoveEntity(entity, null);
            }
            catch
            {
                return;
            }
        }

        protected void QuestAuditBatch(String[] quesIDs)
        {
            QuestionBroker broker = new QuestionBroker();
            foreach (string oid in quesIDs)
            {
                QuestionEntity entity = broker.GetQuestionEntityByOID(oid);
                if (entity != null)
                {
                    entity.Status = QuestionStatus.Unsolve;
                    broker.SaveEntity(entity, null);
                }
            }
        }

        protected void QuestDeleteBatch(String[] quesIDs)
        {
            QuestionBroker broker = new QuestionBroker();
            foreach (string oid in quesIDs)
            {
                QuestionEntity entity = broker.GetQuestionEntityByOID(oid);
                if (entity != null)
                {
                    //entity.Status = QuestionStatus.Unsolve;
                    broker.RemoveEntity(entity, null);
                }
            }
        }
    }
}
